### Alizade, Dancygier, Ditlmann 
### "National Penalties Reversed"
### Replication Code 
### Figure 2
### For questions, contact jalizade@princeton.edu

# empty environment
rm(list = ls())

#setwd("")
setwd("C:/Users/Jey/Dropbox/WZB/NaturalizationExperiment/Submission/JOP/replication_JOP/data")

# load necessary packages
library(readstata13)
library(plotrix)
library(ggplot2)

# load data set
dat <- read.dta13("data_experimental.dta")

# convert treatment variable from second experiment to factor
dat$e2_treat[dat$e2_treat=="NA"] <- NA
dat$e2_treat <- factor(dat$e2_treat, levels=c("Vote Intention (Canadian)", "Control (Turkish)", "Vote Intention (Turkish)", "Integration Problems (Turkish)"))

### figure ###

# generate data frame with means, SEs, and N by treatment condition
df <- data.frame(aggregate(dat$e2_response, list(dat$e2_treat), FUN = mean),
                 aggregate(dat$e2_response, list(dat$e2_treat), FUN = std.error)[,2],
                 aggregate(dat$e2_response, list(dat$e2_treat), FUN = length)[,2])
names(df) <- c("treat", "rr", "se", "N")


# reorder factor levels of treatment variable
df$treat <- factor(df$treat, levels = c("Control (Turkish)", "Vote Intention (Canadian)",
                                        "Vote Intention (Turkish)", "Integration Problems (Turkish)"))

# generate indicator of nationality of the treatment
df$nat <- c("Canadian", rep("Turkish", 3)) # the first is Canadian, the next three are Turkish

# paste "N = " and the number of observations by treatment group
df$N <- paste0("N = ", df$N) 

# generate the figure
fig2 <- ggplot(df, aes(x = treat, y = rr))
fig2 <- fig2 + geom_errorbar(aes(ymax = rr + se, ymin = rr - se, color = nat), 
                             size = 1.2, position = position_dodge(width = 0.5), width = 0)
fig2 <- fig2 + geom_errorbar(aes(ymax = rr + 1.96*se, ymin = rr - 1.96*se, color = nat), 
                             size = 0.4, position = position_dodge(width = 0.5), width = 0.2)
fig2 <- fig2 + geom_point(aes(x = treat, y = rr, shape = nat, fill = nat, color = nat), 
                          position = position_dodge(width = 0.5), size = 3)
fig2 <- fig2 + geom_text(aes(x = treat, y = (rr + 1.96*se) + 0.01, label = N), size = 5)
fig2 <- fig2 + xlab("") + ylab("Response Rate")
fig2 <- fig2 + scale_fill_manual(values = c("black", "red"), name = "Nationality")
fig2 <- fig2 + scale_color_manual(values = c("black", "red"), name = "Nationality")
fig2 <- fig2 + scale_shape_manual(values = c(22, 24), name = "Nationality")
fig2 <- fig2 + scale_x_discrete(labels = c("Control", "Vote Intention", "Vote Intention", "Integration\nProblems"))
fig2 <- fig2 + scale_y_continuous(breaks = seq(0.2, 0.4, 0.05))
fig2 <- fig2 + theme_bw()
fig2 <- fig2 + theme(text = element_text(size = 24), panel.grid = element_blank())
